package com.lvyou.intercepter;

import com.lvyou.entity.UserModel;
import com.lvyou.util.UrlKit;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserLoginInter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean res=false;
        HttpSession session=request.getSession();

        UserModel um=(UserModel)session.getAttribute("userInfo");
        if(um!=null) {
            res=true;
        }else {
            String servletPath = request.getServletPath().toLowerCase();
//            if("/order/check".equals(servletPath)) {
//                String url= UrlKit.getURL(request);
//                response.sendRedirect(url+"/login.html");
//                res=false;
//            }else if(servletPath.matches("/order/.*.html")) {
//                String url=UrlKit.getURL(request);
//                response.sendRedirect(url+"/login.html");
//                res=false;
//            }
            if(servletPath.endsWith(".html")){
                response.sendRedirect("/login.html");
            }else {
                request.getRequestDispatcher("/toLogin").forward(request,response);
                res = false;
            }
        }
        
        return res;
    }
}
